package com.zys.el.search.controller;

import com.zys.el.core.po.ResponseBean;
import com.zys.el.search.po.SearchRequest;
import com.zys.el.search.po.SearchResult;
import com.zys.el.search.service.AnticleService;
import com.zys.el.search.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@CrossOrigin
public class SearchController {

    @Autowired
    private SearchService searchService;

    @Autowired
    private AnticleService anticleService;

    @PostMapping("/query")
    public ResponseBean queryAnticleByPage(@RequestBody SearchRequest searchRequest) throws IOException {
        anticleService.buildAnticles();
        SearchResult result = searchService.search(searchRequest);
        if (result == null) {
            return null;
        }
        ResponseBean responseBean = new ResponseBean();
        responseBean.setModel(result.getNews());
        return responseBean;
    }
}
